<?php
/**
 * 专题碎片管理
 */

if(!defined('IN_SITE') || !defined('IN_ADMIN')) {
	exit('Access Denied');
}
function searchFilter($str) {
	//转义引号
	if(!get_magic_quotes_gpc()) {
		$str = addslashes($str);
	}
	
	//过滤MySQL特殊符号
	$str = str_replace('[', '[[]', $str);
	$str = str_replace('_', '[_]', $str);
	$str = str_replace('%', '[%]', $str);
	
	//多个关键字
	if(preg_match('/ /', $str)) {
		$str_arr = array();
		$str_arr = explode(" ", $str);
		return $str_arr;
	}else{
	//一个关键字
		return $str;
	}
}

$block = load("block.class.php");

$op = in_array($op, array('list', 'add', 'edit', 'update', 'update1', 'del', 'setorder', 'disable', 'refresh', 'get_template_example', 'getpageid')) ? $op : 'list';

switch($op)
{
    case 'add':
		if($dosubmit)
		{
			unset($d[searchpageid]);
			$blockid = $block->add($d);
			if($blockid)
			{
				header('location:?action=block&op=update&blockid='.$blockid);
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		break;
    case 'edit':
		if($dosubmit)
		{
			unset($d[searchpageid]);
			$result = $block->edit($blockid, $d);
			if($result)
			{
				header('location:?action=block&op=update&blockid='.$blockid);
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		else
		{
			$data = $block->get($blockid);
			if(!$data) showmessage('指定的碎片不存在！');
		}
		break;

    case 'update':
		$r = $block->get($blockid);
		if(!$r) showmessage('指定的碎片不存在！');

		if($dosubmit)
		{
			if(is_array($data)) $block->set_template($blockid, $template);
			$result = $block->update($blockid, $data);
			if($result)
			{
				showmessage('操作成功！', "?action=block&op=update&blockid=$blockid");
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		else
		{
			extract($r);
			$template = $block->get_template($blockid);
		}
		break;
	
	case 'update1':
		$blockid = $block->add($d);
		if($blockid)
		{
			$data= "<iframe width=\"620\" height=\"550\" frameborder=\"0\" src=\"?action=block&op=update&blockid=$blockid\"></iframe>";
		}
		else
		{
			exit('2');
		}
		header("Content-type: text/html; charset=GBK");
		exit($data);

    case 'del':
		$result = $block->delete($blockid);
		if($result)
		{
			showmessage('操作成功！', '?action=block');
		}
		else
		{
			showmessage('操作失败！');
		}
		break;

    case 'setorder':
		$result = $block->listorder($listorder);
		if($result)
		{
			showmessage('操作成功！', '?action=block');
		}
		else
		{
			showmessage('操作失败！');
		}
		break;

    case 'disable':
		$result = $block->disable($blockid, $disabled);
		if($result)
		{
			showmessage('操作成功！', '?action=block');
		}
		else
		{
			showmessage('操作失败！');
		}
		break;

	case 'refresh':
        @set_time_limit(600);
	    $block->refresh();
		showmessage('操作完成！', '?action=block');
		break;

    case 'list':
		$data = $block->listinfo('', $page);
		$pageurl = $block->pages;
		break;
		
	case 'get_template_example':
		if($example)
	    {
	    	header("Content-type: text/html; charset=GBK");
		    exit(htmlspecialchars($block->get_template_example($example)));
		}
		else
		{
			$data = $block->get_template_example();
			$template_select = "<select name=\"example\" id=\"example\"  size=\"1\"  style=\"width:100px;\" onchange=\"$('#template').load('?action=block&op=get_template_example&example='+this.value);\">";
//			$template_select .= selectradio('', '', $data, '', 'option');
//			$template_select .= "</select>";
			foreach($data as $key=>$val){
				$template_select .= "<option value=\"$key\" >".$val."</option>\r\n";
			}
			$template_select .= "</select>";
			header("Content-type: text/html; charset=GBK");
			exit($template_select);
		}
		break;
	case 'getpageid':
		$searchkey = $searchkey ? trim($searchkey) : '';
//		搜索的关键字不能为空
		if(empty($searchkey)){
			exit(1);
		}
//		
//		搜索字数限制
		$maxkeywords = $maxkeywords ? $maxkeywords : 20;
		if(strlen($searchkey) > $maxkeywords) {
			exit(2);
		}
//		对关键字进行过滤
		$keywords = iconv('UTF-8', 'GBK', searchFilter($searchkey));
		
		$sql = "SELECT contentid,title FROM `{$tablepre}content` WHERE title LIKE '%$keywords%' AND special=1 LIMIT 0, 500";
		$query = $db->query($sql);
		while ($row = $db->fetch_array($query)) {
			$data[] = $row;
		}
		if(!empty($data))
		{
			foreach($data as $k=>$v){
				$html .= "<a href=\"javascript:\" onclick=\"insertsid($v[contentid])\">$v[title]</a>&nbsp<a href=\"javascript:\" onclick=\"insertsid($v[contentid])\"><font color=\"red\">点击插入页面标识符</font></a><br>";
			}
		}
		header("Content-type: text/html; charset=GBK");
		exit($html);
		break;
}

include admin_tpl("block");
?>